ceTe Software Help Library for Java August - 2020
DynamicPDF Merger for Java / Programming with Merger for Java / Document Optimization
In This Topic
    Document Optimization
    In This Topic

    In DynamicPDF Merger there are several ways to optimize a document; combining duplicate images, stripping out unneeded content, setting a high compression level or setting a document to be linearized (optimized for fast web viewing only). These items are discussed below.

    Combining Images

    One way to achieve an optimized PDF is to tell Merger to combine the image data of any images that are shared throughout one or more PDF documents. In other words, if there was an Image_A inside PDF_A and the identical Image_A was inside PDF_B, then when you merge PDF_A and PDF_B merger can combine the image data content into one therefore reducing the overall file size of the output PDF. Setting a high compression level will also further increase the file size reduction potential.

    The code below demonstrates how to combine a PDF's resources (Images) along with setting a high CompressionLevel.

    [Java]
        DocumentOptimization optimization = new DocumentOptimization (true);
        document.setOptimization(optimization);
        document.setCompressionLevel(9);
    

    Stripping Unneeded Content

    Another way to potentially further reduce the file size would be to remove any content of the PDF document that is being merged in that is not needed. A PDF content may contain JavaScript, Bookmarks, Meta Data, Logical Structure, Embedded Files, Form Fields, etc. all of which may not be a required element in the final output PDF. Therefore, removing them from the document as they are merged in has the potential of reducing the file size. All this is achieved by using the MergeOptions class which allows users to set exactly what is and is not merged into the file being created.

    The code below demonstrates how to exclude JavaScript, bookmarks, metadata, logical structure, embedded files from the document.

    [Java]
        MergeOptions mergeOptions = new MergeOptions();
        mergeOptions.setLogicalStructure(false);
        mergeOptions.setOutLines(false);
        mergeOptions.setDocumentJavaScript(false);
        mergeOptions.setEmbeddedFiles(false);
        MergeDocument document = new MergeDocument("[PhysicalPath]/Document1.pdf",mergeOptions);
        document.draw("[PhysicalPath]/MyDocument.pdf");
    

    Linearizing the PDF (Fast Web View)

    Setting the PDF file to be linearized will not optimize it in terms of file size (in fact, a linearized PDF will be just slightly larger than its non-linearized counterpart) but it allows the PDF, once on the server, to serve up pages to the browser as they are become available (as opposed to waiting for the entire file to be downloaded).

    We can also optimize the PDF for fast web view using the below code.

    [Java]
        MergeDocument.setDefaultPdfFormat(PdfFormat.LINEARIZED);